package epoverty;

import java.util.ArrayList;

/**
 * This class receives an array and formats it for
 * the Combo Boxes used in the project.
 *
 * @author Tyrel Colt
 */
public class ArraySorter
{
   // GETS RID OF UNNECESSARY CHARACTERS AND INFORMATION IN COMBO BOXES
   public static ArrayList<Object> sortArray(ArrayList<Object> array)
   {
      ArrayList<Object> newArray = new ArrayList<Object>(array.size());
      Object[] stringArray = array.toArray();

      // WILL SORT THROUGH THE ARRAY AND GET RID OF THE BRACKETS AND ID NUMBERS
      for (int x = 0; x < array.size(); x++)
      {
         String data = stringArray[x].toString();
         newArray.add(data.substring(1, data.lastIndexOf(',')));
      }

      return newArray;
   }
   
   // SEARCHES AN ARRAYLIST FOR A MATCHING STRING AND RETURNS THE ID FROM THE ARRAYLIST
   public static int idFinder(ArrayList<Object> array, String stringToMatch)
   {
      int id = -1;
      // GO THROUGH EVERY ENTRY IN THE ARRAYLIST
      for (int x = 0; x < array.size(); x++)
      {
         // IF A MATCH IS FOUND PARSE OUT THE ID AND RETURN IT
         if (array.get(x).toString().contains(stringToMatch))
         {
            String found = array.get(x).toString();
            id = Integer.parseInt(found.substring(found.lastIndexOf(',') + 2, found.length() -1));
            return id;
         }
      }
      return id;
   }   
}
